<%@page language="java"  import="com.scalar.chiptrack.commons.Constants,
                                 com.scalar.chiptrack.utils.ConfigManager" %>

<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<%@ taglib uri="/WEB-INF/chiptrack-tags.tld" prefix="chiptrack" %>

<chiptrack:checkLogon sourcePage="/purchaseOrderReceipt.do"/>

<%
    String chipTracContext = "/" + ConfigManager.getChipTracContext();
    int purchaseOrderLineItemNumber = -1;
	int index = 0;
    try
	{
        purchaseOrderLineItemNumber = Integer.parseInt( request.getParameter("purchaseOrderLineItemNumber") );
		index = Integer.parseInt( request.getParameter("index") );
    }
	catch(NumberFormatException nfe){ }
%>

<HTML><HEAD><TITLE>Scalarsoft - ChipTrac</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
<script src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>
<script src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<script language="javascript">
var noOfLots = 0;
function addPurchaseOrderLineItemLotDataEntry( purchaseOrderLineItemNumber, index )
{
    document.purchaseOrderReceiptForm.action='<%=chipTracContext%>/purchaseOrderReceipt.do?action=purchaseOrderLotEntryAddMore&purchaseOrderLineItemNumber=' + purchaseOrderLineItemNumber + '&index=' + index;
    document.purchaseOrderReceiptForm.submit();
}

function SaveAndClose()
{
    var totalQty = 0;
    //var lineItemLotObj = document.purchaseOrderReceiptForm.lineItemLot;
    var tmpErrMsg = '';
    var errorIndex = document.getElementById("errorIndex");
    var rowCnt = 0;
    var i;
    //alert ( noOfLots )
    for ( i = 0; i < noOfLots/*lineItemLotObj.length*/; i++ )
    {
        var qtyIn = parseInt ( document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList['+i+'].qtyPulled'].value);
        //var qtyOut = parseInt ( document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList['+i+'].qtyOut'].value);
        var lotNumber = document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList['+i+'].lotNumber'].value;

        if( isNaN( qtyIn ) )
        {
            qtyIn = 0;
        }
       /* if( isNaN( qtyOut ) )
        {
            qtyOut = 0;
        }*/
        if ( lotNumber == '' && qtyIn == 0 /*&& qtyOut == 0*/ )
        {
            rowCnt ++;
            continue;
        }
        if ( document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList[' + i + '].selected'].checked  )
        {
			tmpErrMsg = validate( lotNumber, qtyIn, 0/*qtyOut*/ );
			if( tmpErrMsg != '' )
			{
				errorIndex.innerHTML = tmpErrMsg + " for row "+eval(i+1);
				errorIndex.focus();
				return;
			}
		}

        if ( document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList[' + i + '].selected'].checked  )
        {
            totalQty  = totalQty +  parseInt ( document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList[' + i + '].qtyPulled'].value );
        }
    }
    if( rowCnt == i )
    {
        errorIndex.innerHTML = "<li>Atleast one row should contain valid data";
        errorIndex.focus();
        return;
    }

    if ( isNaN( totalQty ) )
    {
        totalQty = 0;
    }
    var allowedShippedQty = parseInt( window.opener.document.purchaseOrderReceiptForm['allowedShippedQty[<%=index%>]'].value );
    if ( isNaN( allowedShippedQty ) )
    {
        allowedShippedQty = 0;
    }   

   <logic:equal name="purchaseOrderReceiptForm" property="poType" value="<%=Integer.toString(Constants.PO_TYPE_STANDARD)%>" >
    if ( totalQty <= allowedShippedQty )
    {
        window.opener.document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].shippedQty'].value = totalQty;
    }
    else
    {
        errorIndex.innerHTML = "<li>Total Quantity-In must be less than or equal to "+allowedShippedQty;
        errorIndex.focus();
        return;
    }
    </logic:equal>
    <logic:notEqual name="purchaseOrderReceiptForm" property="poType" value="<%=Integer.toString(Constants.PO_TYPE_STANDARD)%>" >
        window.opener.document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].shippedQty'].value = totalQty;
    </logic:notEqual>

    window.opener.calculateTotals();
    for ( i = 0; i < noOfLots; i++ )
    {
        if ( document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList[' + i + '].selected'].checked == false  )
        {
            document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList[' + i + '].selected'].value = false;
            document.purchaseOrderReceiptForm['receivingLineItemList[<%=index%>].purchaseOrderLineItemLotDataEntryList[' + i + '].selected'].checked = true;
        }
    }
    document.purchaseOrderReceiptForm.action='<%=chipTracContext%>/purchaseOrderReceipt.do?action=purchaseOrderLineItemLotEntryClose';
    document.purchaseOrderReceiptForm.submit();
}
function validate( lotNumber, qtyIn, qtyOut )
{
    if( lotNumber != '' )
    {
        if ( qtyIn == 0 )
        {
          return "<li>Enter valid Quantity-In"
        }
        /*if ( qtyIn < qtyOut )
        {
            return "<li>Quantity-In cannot be less than Quantity-Out"
        }*/
    }
    else if( qtyIn != 0 )
    {
        if( lotNumber == '' )
        {
            return "<li>Lot Number cannot be blank if Quantity-In is entered"
        }
       /* if ( qtyIn < qtyOut )
        {
            return "<li>Quantity-In cannot be less than Quantity-Out"
        }*/
    }
   /* else if( qtyOut != 0 )
    {
        return "<li>Please enter Lot Number and Quantity-In"
    }*/
    return "";
}

</script>
</HEAD>
<BODY bgColor=#ffffff leftMargin=0 topMargin=0 marginheight="0" marginwidth="0">
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
    <TBODY>
    <TR>
	    <td width='180' valign='top' background='<%=chipTracContext%>/jsp/includes/images/left_red_bg.gif'>
        <TD bgcolor="#ffffff">
            <IMG height=9 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=8></TD>
        <TD bgcolor="#ffffff"><IMG height=480 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=2></TD>
        <TD valign="top" width="100%">
            <IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/clear.gif" width=500 border=0>
            <BR>
<!-- from here your body starts -->
<html:form name="purchaseOrderReceiptForm"  type="com.scalar.chiptrack.finance.openpo.forms.PurchaseOrderReceiptForm" action="/purchaseOrderReceipt.do">

             <table border="0" align = "left" width ="100%" >
             <TR>
                    <TD WIDTH="100%">
                        <TABLE BORDER="0" WIDTH="100%" HEIGHT="175%">
                            <TR>
                                <TD WIDTH="100%" align = "">
                                    <table width="75%" ALIGN="CENTER"><tr><td><center><FONT id="poErrors" styleClass="NArialL"><b>Purchase Order Line Item Lot Data Entry</b></FONT></center></td></TR></table>
                                </TD>
                            </TR>
                        </TABLE>
                        <BR>
                        <TABLE BORDER="0" ALIGN="CENTER" WIDTH="75%" CLASS="NArialL">
                            <tr>
                                <td colspan='3' class='NArialL' style='color:red' id="errorIndex"></td>
                            </tr>
                            <tr>
                                <td colspan='3'></td>
                            </tr>
                            <TR>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Select</FONT>
                                </TD>
                                <TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Lot Number&nbsp;*</FONT>
                                </TD>
                                <TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Quantity In&nbsp;*</FONT>
                                </TD>
								<TD WIDTH="25%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER">&nbsp;<FONT COLOR="white">Quantity Invoiced&nbsp;*</FONT>
                                </TD>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER"><FONT COLOR="white">Lot Status</FONT>
                                </TD>
                                <TD WIDTH="10%" HEIGHT="26" ALIGN="CENTER" BGCOLOR="#993333">
                                    <P ALIGN="CENTER"><FONT COLOR="white">PO Number</FONT>
                                </TD>
                            </TR>
                            <%
                                boolean disable = true;
                                int noOfLots = 0;
                            %>
                            
                            <nested:iterate id="recevingLineItem" property="receivingLineItemList" type="com.scalar.chiptrack.finance.openpo.ReceivingLineItem">
                                <nested:equal name="recevingLineItem" property = "purchaseOrderLineItemNumber"  value = "<%= String.valueOf( purchaseOrderLineItemNumber )%>" >
                                    <nested:hidden property="purchaseOrderLineItemNumber"/>
                                    <nested:iterate id = "purchaseOrderLineItemLotDataEntry" property="purchaseOrderLineItemLotDataEntryList" type="com.scalar.chiptrack.finance.openpo.PurchaseOrderLineItemLotDataEntry">
                                        <%
                                            noOfLots++;
                                        %>
                                    <nested:empty property="lotNumber">
                                        <%
                                            disable = false;
                                        %>
                                    </nested:empty>
                                    <TR>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:checkbox property="selected" styleClass="txfield"/></P>
											<%
											//System.out.println(purchaseOrderLineItemLotDataEntry.isSelected()+"	"+purchaseOrderLineItemLotDataEntry.getLotNumber());
											 %>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:text property="lotNumber" styleClass="txfield" size="19" maxlength="50" disabled="<%=disable%>" />
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:text property="qtyIn" styleClass="valuetxfield" size="19" onkeypress="javascript:allowInteger()" maxlength="6" disabled="<%=disable%>"/>
                                            </CENTER>
                                        </TD>
										<TD WIDTH="25%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:text property="qtyPulled" styleClass="valuetxfield" size="19" onkeypress="javascript:allowInteger()" maxlength="6" disabled="false"/>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:write property="status"/>
                                            </CENTER>
                                        </TD>
                                        <TD WIDTH="10%" ALIGN="CENTER" BGCOLOR="#DEE1E2">
                                            <CENTER>
                                            <P><nested:write property="poNumber"/>
                                            </CENTER>
                                        </TD>
                                    </TR>
                                    </nested:iterate>
                                </nested:equal>
                            </nested:iterate>
                            <script type="text/javascript">
                                noOfLots = <%=noOfLots%>;
                            </script>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR align = "center" border="0">
                                <TD  bgColor= "" align="right" colspan = '5'>
                                <% String jsMethod = "javascript:addPurchaseOrderLineItemLotDataEntry('" + purchaseOrderLineItemNumber + "','"+ index + "')" ; %>
                                <A href="<%=jsMethod%>"><img border="0" src="<%=chipTracContext%>/jsp/includes/images/addmore.gif" onmouseover="window.status='Click to Create PO';return true" onmouseout="window.status='';return true" title="Click here to Create PO"></A>&nbsp;&nbsp;&nbsp;

                                </TD>
                            </TR>
                            <TR><TD>&nbsp;</TD></TR>
                            <TR align = "center" border="0">
                                <TD  bgColor= "" align="center" colspan = '3'>
                                <A href="javascript:SaveAndClose()" ><img border="0" src="<%=chipTracContext%>/jsp/includes/images/save.gif" width="53" height="17" onmouseover="window.status='Click to Create PO';return true" onmouseout="window.status='';return true" title="Click here to Create PO"></A>&nbsp;&nbsp;&nbsp;
                                <A href="javascript:window.close()" onMouseOver="window.status='Click to Reset the PO Information';return true" onMouseOut="window.status='';return true" title="Click here to Reset Process Information"><IMG border=0 src="<%=chipTracContext%>/jsp/includes/images/cancel.gif"></a>
                                </TD>
                            </TR>
                        </TABLE>
                        <BR>
              </TABLE>
</html:form>
        <BR><BR><BR>
        </TD>
        <TD bgcolor="#ffffff"><IMG height=1 src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" width=1></TD>
    </TR>
</TABLE>
</BODY>
</HTML>
